

from pathlib import Path
import time
import threading
from flask import current_app


class Pager():
    def __init__(self, offset: int, limit: int) -> None:
        self.offset = offset or 0
        self.limit = limit if limit is not None and limit > 0 else current_app.config[
            'PAGER_LIMIT']


def setTimeout(func, secs):
    def temp(func, secs):
        time.sleep(secs)
        func()
    threading.Thread(target=temp, args=(func, secs)).start()


def getProjectRootPath(file__, name__, relative=True):
    """ 
    功能: 获取 main 文件所在的路径, 即项目路径
    file__: 调用函数所属文件的 __file__
    name__: 调用函数所属文件的 __name__
    relative: 返回相对路径 if True else 绝对路径
    """
    print(f'__file__: {file__}\n__name__: {name__}')
    file_path = Path(file__)
    if not file_path.is_absolute():
        file_path = file_path.absolute()
    relative_file_path = file_path.relative_to(Path('.').absolute()) # 获取相对路径
    back_nums = len(name__.split('.')) # 返回上级目录的次数
    if  '__init__' in file_path.name: # 如果是包的初始化文件， 会自动少算一级，那么需要再往上走一级
        back_nums += 1
    project_path = relative_file_path
    for i in range(back_nums):
        project_path = project_path.parent
    # 确定返回的路径格式
    project_path =  project_path if relative else project_path.absolute()
    # 转字符串对象
    project_path = str(project_path)
    # 统一格式
    if project_path == '.':
        project_path = ''
    return project_path